package com.tree.array;

import com.alibaba.fastjson.JSON;

import java.util.LinkedList;

/**
 * package com.tree.array
 * description:
 *
 *  给定2个有序的链表的头指针head1，head2，打印2个链表的公共部分
 *
 *  思路：
 *  1. 如果head1 < head2 , head1 后移
 *  1. 如果head1 > head2 , head2 后移
 *  1. 如果head1 = head2 , 打印， 然后一起后移
 *  1. 如果head1 、head2任意一个为null，结束
 *
 *  链表：https://www.cnblogs.com/llfy/p/9395936.html
 * @author tree
 * @date 2019-10-11 16:22
 */
public class PrintCommonPart {
    static Node head1 = null;
    public static void main(String[] args) {

        add(2);
        add(3);
        add(4);
        System.out.println("====:"+(10+(10>>2)));
        System.out.println(JSON.toJSONString(head1));
        LinkedList l = new LinkedList();
        l.add(1);
        l.add(2);
        System.out.println(JSON.toJSONString(l));
    }

    public static void add(int value){
        Node node = new Node(value);
        if(head1 == null){
            head1 = node;
            return;
        }
        Node tmp = head1;
        while (tmp.next!= null){
            tmp = tmp.next;
        }
        tmp.next= node;
    }

    public static void print(Node head1, Node head2){
        System.out.println("相同的部分是：");
        while (head1!= null || head2 != null){
            if(head1.value < head2.value){
                head1= head1.next;
            }else if(head1.value > head2.value){
                head2 = head2.next;
            }else if(head1.value == head2.value){
                System.out.println(head1.value+",");
            }
        }
    }
}

class Node{
    public int value;
    public Node next;

    public Node(int value){
        this.value = value;
    }


}